package com.List.ArrayList;

import com.sh.Iterator;

import java.util.ArrayList;
import java.util.Arrays;


/**
 * @Auther: sh
 * @Version 1.0
 * @Description: 手写ArrayList的实现类
 */
public class ShArrayList<E> implements com.List.List<E> {
    private static final int DEFAULT_CAPACITY = 10;
    private Object[] elements;
    private int size = 0;

    public ShArrayList() {
        this.elements = new Object[DEFAULT_CAPACITY];
    }

    //扩容方法
    private void ensureCapacity() {
        if (size >= elements.length) {
            int newLength = elements.length + (elements.length >> 1);
            elements = Arrays.copyOf(elements, newLength);
        }
    }

    // 检查范围
    private void rangeCheckForAdd(int index) {
        if (index > size || index < 0)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

    private String outOfBoundsMsg(int index) {
        return "Index: " + index + ", Size: " + size();
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean add(E element) {
        size++;
        if (size == elements.length) {
            ensureCapacity();
        }
        elements[size] = element;
        return true;
    }

    // 转型
    @SuppressWarnings("unchecked")
    private E elementData(int index) {
        return (E) elements[index];
    }

    @Override
    public E get(int index) {
        rangeCheckForAdd(index);
        return elementData(index);
    }

    @Override
    public E set(int index, E element) {
        return null;
    }

    @Override
    public E remove(int index) {
        return null;
    }

    @Override
    public boolean remove(Object o) {
        return false;
    }

    @Override
    public void clear() {

    }

    @Override
    public boolean contains(Object o) {
        return false;
    }
}

